# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\licensing\__init__.py
import time, json, logging, os.path, threading
from helpers.licensing.erp_errors import LocalLicenseRefreshError
from settings_provider import settings
from scanners.wvs.wvs_process import WVSLicenseTimeout, WVSExecutionError, WVSProcess

__logger = logging.getLogger("helpers.licensing")
last_license_refresh = 0
license_refresh_delta = 600
license_lock = threading.RLock()
use_mock_data = False


def reset_license_refresh():
    global last_license_refresh
    last_license_refresh = 0


def get_local_license(force_refresh=False):
    global last_license_refresh
    with license_lock:
        current_time = time.time()
        if force_refresh or current_time - last_license_refresh > license_refresh_delta:
            pass
        if not use_mock_data:
            __logger.debug("refreshing license...")
            try:
                wvs_process = WVSProcess(
                    settings.get(
                        "wvs.app_dir",
                        default=os.path.normpath(
                            os.getcwd() + os.path.sep + ".." + os.path.sep
                        ),
                    ),
                    wvs_settings=None,
                    working_directory=None,
                )
                wvs_process.refresh_wvs_license(timeout=10)
            except WVSLicenseTimeout:
                __logger.error("refreshing license WVSLicenseTimeout")
            except WVSExecutionError:
                __logger.error("refreshing license WVSExecutionError")
            else:
                __logger.debug("refreshing success")
                last_license_refresh = current_time

            license_info = None
            if use_mock_data:
                license_file_path = os.path.join(
                    settings.get("storage.license", ""), "license_info_mock.json"
                )
            else:
                license_file_path = os.path.join(
                    settings.get("storage.license", ""), "license_info.json"
                )
            if os.path.isfile(license_file_path):
                try:
                    license_info = json.loads(
                        (open(license_file_path, "rt", encoding="utf-8")).read()
                    )
                except Exception as e:
                    __logger.error("reading license file failed with %s", e)

            else:
                __logger.error("license file not found")
            if not license_info:
                __logger.error("empty license")
                license_info = dict()
                last_license_refresh = 0
            return dict(license_info)


def get_bxss_credentials():
    l = get_local_license()
    return (l["bxss_api_key"], l["bxss_user_id"])
